# arthor vuong.quoc
#! C:/Perl/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Juniper;
use TestCases;
#use autodie;
my @data;
sub readpara
{	

	my @fileName;
	#print "Input path : ";
	my $path = "D:/param";#<STDIN>;
	chomp($path);
	opendir (DIR, $path) or die $!;
	while(my $file = readdir(DIR))
	{
		if($file !~ m/^\./)
		{
			push(@fileName,$file);
		}
	}                                      
	closedir DIR;
	foreach my $file (@fileName)
	{
		my $topology;
		my $variable;
		my $testcase;
		my $paramsDefault;
		my $routerConfig;
		open File, ("+<$path"."/"."$file") or die $!;
		my @list = <File>;
		my $size = @list;
		my $i;
		my $flag = 0;
		my $row ="";
		for($i=0;$i<$size;$i++)
		{	
			
			# remove space & comment
			$list[$i] = &removeComment($list[$i]);
			# Read file to obj
			if($list[$i] =~ m/x-test-topology/)
			{
				while($list[$i] !~ m/\;/)
				{
					# $list[$i] = &removeComment($list[$i]);
					$topology = $topology."$list[$i]";
					$i++;
				}
				$topology = $topology."$list[$i]";
				
			}
			elsif($list[$i] =~ m/x-testcase-/)
			{
				while($list[$i] !~ m/\;/)
				{
					$list[$i] = &removeComment($list[$i]);
					$testcase = $testcase."$list[$i]";
					$i++;
				}
				$testcase = $testcase."$list[$i]";
				
			}
			elsif($list[$i] =~ m/x-/ && $list[$i] !~ m/x-testcase-/ && $list[$i] !~ m/x-test-topology/)	
			{
				while($list[$i] !~ m/\;/)
				{
					$list[$i] = &removeComment($list[$i]);
					$variable = $variable."$list[$i]";
					$i++;
				}
				$variable = $variable."$list[$i]";
			}elsif($list[$i] =~ m/params-default/)
			{
				# $paramsDefault = $paramsDefault."BEGINPram\n";	
				# print $list[$i];
				my $closep = 0 ;
				my $openp = 0;
				my $flagp = 0;
				while($flagp < 1 )
				{	
					$list[$i] = &removeComment($list[$i]);
					$paramsDefault = $paramsDefault.$list[$i];
					my @countopenp = $list[$i] =~ /\}/g;
					my @countclosep = $list[$i] =~ /\{/g;
					$openp = $openp + scalar @countopenp;
					$closep = $closep + scalar @countclosep;
					if($openp > 0 && $closep > 0 && $openp == $closep)
					{
						$flagp = 1 ;
						# $paramsDefault = $paramsDefault."\nENDPram\n";							
					}
					$i++;
				}	
				$i--;
			}	
			elsif($list[$i] =~ m/^r/ || $list[$i] =~ m/^h/)
			{
				$routerConfig = $routerConfig."BEGIN\n";	
				# print $list[$i];
				my $close = 0 ;
				my $open = 0;
				my $flag = 0;
				while($flag < 1 )
				{	
					$list[$i] = &removeComment($list[$i]);
					$routerConfig = $routerConfig.$list[$i];
					my @countopen = $list[$i] =~ /\}/g;
					my @countclose = $list[$i] =~ /\{/g;
					$open = $open + scalar @countopen;
					$close = $close + scalar @countclose;
					if($open > 0 && $close > 0 && $open == $close)
					{
						$flag = 1 ;
						$routerConfig = $routerConfig."\nEND NOW\n";							
					}
					$i++;
				}	
				$i--;
			}			
		}
		# get data
		my $object = new Juniper($file, $variable,$paramsDefault, $testcase, $topology, $routerConfig);
		push(@data, $object);
		# Destroy
	}
	print "Size: ",scalar @data,"\n";
	# print $data[20]->showAll();
}
# show all data 
sub ShowAllData
{
	foreach my $file (@data)
	{
		# print $file->getFileName(),"\n";
		# $file->getVariable();
		# print $file->getParamsDefault();
		print $file->getTestCase();
		# print $file->getTopology();
		# print $file->getRouterConfig()
	}
}
# Remove all Space in first and in the end.
sub regex_or {
  my $string = shift;
  $string =~ s/(?:^\s+)||(?: +$)//g;
  return $string;
}

sub regex_all {
  my $string = shift;
  $string =~ s/(?:^\s+)||(?:\s+$)//g;
  return $string;
}
sub for_plus {
	my $string = shift;
	for ($string) { s/^\s*//; s/\s*$//; }
	return $string;
}
sub double_star {
	my $string = shift;
	$string =~ s/^\s*//;
	$string =~ s/\s*$//;
	return $string;
}
# Remove all space and comment
sub removeComment
{
  my $string = shift;
  my @results = split("#",$string);
  $string =regex_or($results[0]);
  return $string;
}
# quoc.le
# Read all test case in a Object
# Return array obj Testcase
sub readTestCases()
{
	my @all_test_cases;  
	my @test_case;
	my $valTC = shift;
	my @valsTC = split(';',$valTC);
	for my $itemTC (@valsTC)
	{
		# print $itemTC,"\nEND\n";
		$itemTC = regex_or($itemTC);
		my $name = substr ($itemTC,0,15);
		if(length($itemTC) > 15)
		{
			$itemTC = substr ($itemTC,15);
			# print "Name : ",$name, "\n";
			# print "values : ",$itemTC, "\n";
			my $objectTC = new TestCases($name,$itemTC);
			push(@test_case,$objectTC);
			# print $objectTC->getNames(),"\n";
			# $objectTC->getValues();
		}
		
	}
	
	return @test_case;
}
#function reardpara()
&readpara();
#&ShowAllData();
# print $data[2]->getFileName(),"\n";
&readTestCases($data[2]->getTestCase);

# my $stop = <STDIN>;
